#include <model3d.h>
#include <scn.h>
#include <vector3.h>
#include <matrix4x4.h>
#include <transforms.h>
#include <render.h>

#include <visual.h>
#include <debug.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int
main( int argc, char *argv[] )
{
  model3d_t *model;
  surface_t *surface;

  debug_level = DEBUG_DBG; //teste

  if ( argc < 2 )
    {
      fprintf( stderr, "Missing SCN file.\n" );
      return -1;
    }


  model = scn_load( argv[ 1 ] );

  uint16_t vmin, vmax;
  find_intensities( model, &vmin, &vmax );

  uint16_t d = sqrt( pow2( model->size.x ) +
		     pow2( model->size.y ) +
		     pow2( model->size.z ) );
  visual_init( d, d );

  surface = surface_new( d, d );

  int x = model->size.x / 2 - 1;
  int y = model->size.y / 2 - 1;
  int z = model->size.z / 2 - 1;

  plane_t planes[] = {
    { .point  = vector3_init(  x,  0,  0 ),
      .normal = vector3_init(  1,  0,  0 ) },
    { .point  = vector3_init(  0,  y,  0 ),
      .normal = vector3_init(  0,  1,  0 ) },
    { .point  = vector3_init(  0,  0,  z ),
      .normal = vector3_init(  0,  0,  1 ) },
    { .point  = vector3_init( -x-1,  0,  0 ),
      .normal = vector3_init( -1,  0,  0 ) },
    { .point  = vector3_init(  0, -y-1,  0 ),
      .normal = vector3_init(  0, -1,  0 ) },
    { .point  = vector3_init(  0,  0, -z-1 ),
      .normal = vector3_init(  0,  0, -1 ) },
    { .point  = vector3_init(  0,  x/2, 0 ),
      .normal = vector3_init(  0,  1, 1 ) },
    { .point  = vector3_init(  0,  x/2, 0 ),
      .normal = vector3_init(  0,  1, -1 ) },
  };

  color_t edge_color, bg_color;
  COLOR_FROM_ARGB( edge_color, COLOR_RED );
  COLOR_FROM_ARGB( bg_color, COLOR_YELLOW );

  double rot_y=-M_PI/10, rot_x=M_PI/8;

  render( surface, 0, 0,
	  model, planes, 8,
	  rot_y, rot_x,
	  &edge_color, &bg_color,
	  vmin, vmax );

  coord_t pos0 = { 0, 0 };
  visual_display( surface, pos0 );
  visual_update();
  visual_waitkey();

  visual_quit();

  model3d_free( model );

  return 0;
}
